/**
 * Copyright ©2016-2020 northkingbpo Corporation, All Rights Reserved
 * http://www.northkingbpo.net/
 */
package profile.hujia.simulation.data.service;

import profile.hujia.simulation.data.common.CommonFunction;
import profile.hujia.simulation.data.common.ExcelFile;
import profile.hujia.simulation.data.config.SystemConfig;
import profile.hujia.simulation.data.domain.Business;
import profile.hujia.simulation.data.domain.Person;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * class info
 *
 * @author hujia
 * @date 2019-06-14 10:49
 */
public class DataSaveToExcel {
    private Integer numberOfPerson;
    private Integer numberOfBusinessType;
    private HashMap<Integer, Person> persons;
    private ExcelFile excelFile;

    public DataSaveToExcel(HashMap<Integer, Person> persons, ExcelFile excelFile) {
        numberOfBusinessType = SystemConfig.TOTAL_BUSINESS_TYPE;
        numberOfPerson = SystemConfig.TOTAL_PERSON_NUMBER;
        this.persons = persons;
        this.excelFile = excelFile;
    }

    public void run() {
        excelSingle();
        excelSingleBusiness();
    }

    private void excelSingle() {
        for (int i = 0; i < SystemConfig.SINGLE_NAME.length; ++i) {
            String labelName = SystemConfig.SINGLE_NAME[i];
            //init
            HashMap<Integer, List<String>> contentValue = new HashMap<>();
            //content
            for (int j = 0; j < numberOfPerson; ++j) {
                contentValue.put(j + 1, new ArrayList<>());
                contentValue.get(j + 1).add(persons.get(j + 1).getPersonName());
                contentValue.get(j + 1).add(persons.get(j + 1).getLabelMap().get(labelName).toString());
            }
            //title
            String title = "操作员名称," + labelName;
            excelPersonValue(labelName, title, contentValue);
        }
    }

    public void excelSingleBusiness() {
        //title
        StringBuilder title = new StringBuilder("操作员名称");
        for (int i = 0; i < numberOfBusinessType; ++i) {
            title.append(SystemConfig.SEPARATION_CHARACTER);
            title.append(String.format("业务种类%02d", i + 1));
        }
        for (int i = 0; i < SystemConfig.SINGLE_BUSINESS_NAME.length; ++i) {
            String labelName = SystemConfig.SINGLE_BUSINESS_NAME[i];
            //init
            HashMap<Integer, List<String>> contentValue = new HashMap<>();

            //content
            for (int j = 0; j < numberOfPerson; ++j) {
                List<String> singleContent = new ArrayList<>();
                singleContent.add(persons.get(j + 1).getPersonName());
                for (int k = 0; k < numberOfBusinessType; ++k) {
                    Business business = persons.get(j + 1).getBusinessAttribute().get(k + 1);
                    if (business == null) {
                        singleContent.add("-");
                    } else {
                        try {
                            Integer value = business.getLabelMap().get(labelName);
                            singleContent.add(business.getLabelMap().get(labelName).toString());
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
                        }
                    }
                }
                contentValue.put(j + 1, singleContent);
            }
            excelPersonValue(labelName, title.toString(), contentValue);
        }

    }

    private void excelPersonValue(String labelName, String title, HashMap<Integer, List<String>> labelValues) {
        excelFile.getSheet("person-" + labelName);
        //set title
        List<String> values = Arrays.asList(title.split(SystemConfig.SEPARATION_CHARACTER));
        excelFile.setRowValue(values);
        //set content
        for (int i = 0; i < numberOfPerson; ++i) {
            excelFile.setRowValue(labelValues.get(i + 1));
        }
    }

}
